package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.constant.SignatureConstant;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmTaskService;
import com.sec.etech.org.service.EtechUserUtil;
import com.sec.etech.presale.service.CRM_PreProjectService;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.org.api.model.IUser;
import org.openbpm.sys.util.ContextUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class IsmNpdfs1ServiceImpl implements IsmMethodService {
    @Resource
    IsmTaskService ismTaskService;
    @Resource
    CRM_PreProjectService preProjecrService;

    public IsmNpdfs1ServiceImpl(){}

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception{
        JSONObject result = new JSONObject();
        JSONObject resData = this.npdfs1(businessParams.getJSONObject(SignatureConstant.INPUT_PARAMS));
        ismTaskService.deleteTaskByParams(sysCode, serviceCode, serviceCode, null,
                businessParams.getString(SignatureConstant.INPUT_PARAMS));
        result.put("flag", "success");
        result.put("data", resData);
        return result;
    }

    //同步售前信息返回项目编号
    private JSONObject npdfs1(JSONObject object) throws Exception{
        String userAccount = object.getString("userAccount");
        if(StringUtil.isEmpty(userAccount)){
            userAccount = "admin";
        }
        IUser user = EtechUserUtil.getUserByAccount(userAccount);
        if(user==null) {
            user = EtechUserUtil.getUserAdmin();
        }
        ContextUtil.setCurrentUser(user);
        String res = preProjecrService.set(object.getString("action"), object.getString("data"));
        JSONObject obj = new JSONObject();
        obj.put("projectNum", res);
        return obj;
    }

    @Override
    public String methodName(){
        return "npdfs1";
    }
}
